package com.xyang.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyang.demo.dao.BxbMapper;
import com.xyang.demo.entity.Bxb;
import com.xyang.demo.entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Stars-Of-Future-xYang
 * @version 1.0
 * @date 2021/3/19 10:22
 */

@Service
public class BxbServiceImpl<T> {

    @Autowired
    private BxbMapper bxbMapper;

    public PageResult findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Bxb> data = bxbMapper.findAll();
        PageInfo<Bxb> pageInfo = new PageInfo<>(data);
        PageResult pageResult = new PageResult(pageInfo.getList(), pageInfo.getTotal());
        return pageResult;
    }

    public void add(Bxb bxb) {
        bxb.setTime(new Date());
        bxb.setIsBx(0);
        bxb.setIsDelete(0);
        bxbMapper.add(bxb);
    }

    public void update(Bxb bxb) {
        bxb.setTime(new Date());
        bxbMapper.update(bxb);
    }

    public void delete(Integer id) {
        bxbMapper.deleteById(id);
    }

    public Double findAllTotalPrice() {
        List<Bxb> list = bxbMapper.findAll();
        AtomicReference<Double> total= new AtomicReference<>(new Double(0));
        list.stream().map(bxb->{
            if (bxb.getIsBx().equals(0)) {
                BigDecimal bigDecimal = new BigDecimal(bxb.getPrice());
                return bigDecimal.doubleValue();
            }
            return 0.0;
        }).forEach(val->{
            total.updateAndGet(v -> v + val);
        });
        return total.get();
    }
}


